import axios from 'axios'
import { ElNotification } from 'element-plus'
const request = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 5000,
})

//请求拦截
request.interceptors.request.use((config) => {
  config.headers.token = 'qwe'

  //返回配置对象
  return config
})
//相应拦截器
request.interceptors.response.use(
  (response) => {
    //成功回调
    //简化数据
    return response.data
  },
  (error) => {
    //失败
    let message: string = ''
    const status = error.response.status
    switch (status) {
      case 401:
        message = 'token过期'
        break
      case 403:
        message = '权限不足'
        break
      case 404:
        message = '请求地址错误'
        break
      case 500:
        message = '系统错误'
        break
      default:
        message = '网络错误'
    }

    ElNotification({
      title: '新消息',
      message: message,
      type: 'error',
    })
    return Promise.reject(error)
  },
)
export default request
